package com.itheima.health.controller;

import java.util.Map;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import com.itheima.health.vo.HotSemeal;
import com.itheima.health.vo.ReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itheima.health.vo.ReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;


/**
 * 运营数据统计
 */
@RequestMapping("/report")
@RestController
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;


    /**
     * 到处运营数据
     */
    @GetMapping("exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        log.info("[导出运营数据报表]开始");
        //1-RPC获取数据
        ReportVO reportData = reportService.getBusinessReportData();
        log.debug("[导出运营数据报表]rpc rsp:{}", reportData);
        // 取出返回结果
        String reportDate = reportData.getReportDate();
        // 获取会员相关数据
        Integer todayNewMember =  reportData.getTodayNewMember();
        Integer thisWeekNewMember =  reportData.getThisWeekNewMember();
        Integer thisMonthNewMember =  reportData.getThisMonthNewMember();
        Integer totalMember =  reportData.getTotalMember();
        // 获取预约相关数据
        Integer todayOrderNumber =  reportData.getTodayOrderNumber();
        Integer thisWeekOrderNumber =  reportData.getThisWeekOrderNumber();
        Integer thisMonthOrderNumber =  reportData.getThisMonthOrderNumber();
        // 获取到诊相关数据
        Integer todayVisitsNumber =  reportData.getTodayVisitsNumber();
        Integer thisWeekVisitsNumber =  reportData.getThisWeekVisitsNumber();
        Integer thisMonthVisitsNumber =  reportData.getThisMonthVisitsNumber();
        // 获取套餐数据
        List<HotSemeal> hotSetmeal =  reportData.getHotSetmeal();


        //2-读取模板构造workBook
        try  {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
            Workbook workbook = new XSSFWorkbook(is);
            OutputStream os = response.getOutputStream();

            Sheet sheet = workbook.getSheetAt(0);
            //3-填入数据
            //日期
            sheet.getRow(2).getCell(5).setCellValue(reportDate);
            //会员统计数据
            sheet.getRow(4).getCell(5).setCellValue(todayNewMember);
            sheet.getRow(4).getCell(7).setCellValue(totalMember);
            sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
            sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
            //预约到诊数据统计
            sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);
            sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
            sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
            sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
            sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
            sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);
            //热门套餐
            int rowNum = 12;
            for (HotSemeal setMeal : hotSetmeal) {
                Row row = sheet.getRow(rowNum);
                row.getCell(4).setCellValue( setMeal.getName());
                row.getCell(5).setCellValue(setMeal.getSetmeal_count());
                row.getCell(6).setCellValue( setMeal.getProportion());
                rowNum++;
            }

            //4-写入网络输出流
            //写入返回流
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;fileName=" + reportDate + "_report.xlsx");
            workbook.write(os);
            return null;
        } catch (IOException e) {
            log.error("",e);
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }


    /**
     * 饼型套餐占比统计
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        List<Map<String, Object>> list = reportService.findSetmealCount();

        Map<String,Object> map = new HashMap<>();
        map.put("setmealCount",list);

        List<String> setmealNames = new ArrayList<>();
        for(Map<String,Object> m : list){
            String name = (String) m.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames",setmealNames);

        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }

    /**
     *
     * 运营数据统计⻚⾯展示
     * @return
     */
    @GetMapping("getBusinessReportData")
    public Result getBusinessReportData(){
        ReportVO reportVO = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS,reportVO);
    }
}
